package com.goktech.sportsmall.service.Impl;


import com.goktech.sportsmall.bean.CommodityBean;
import com.goktech.sportsmall.mapper.CommodityMapper;
import com.goktech.sportsmall.service.CommodityService;
import com.goktech.sportsmall.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired(required = false)
    private CommodityMapper commodityMapper;

    // 查询所有的类别
    @Override
    public Map<String, Object> vueSelectCount() {
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("title","查询商品类别");
        hashMap.put("message","查询成功");
        hashMap.put("data", commodityMapper.vueSelectCount());
        return hashMap;
    }

    // 添加商品
    @Override
    public String addCommodity(CommodityBean commodityBean) {
        List<CommodityBean> comm = commodityMapper.selectByNo(DateUtil.getDate("yyyyMMdd"));
        String cNo = "C"+ DateUtil.getDate("yyyyMMdd");
        if(comm.size()==0){
            cNo=cNo+"0001";
        }else{
            String str =comm.get(0).getC_no().substring(9);
            int no =Integer.valueOf(str);
            no++;
            cNo=cNo+String.format("%0"+4+"d",no);
        }
        commodityBean.setC_no(cNo);

        commodityBean.setC_create_time(DateUtil.getDate("yyyyMMddHHmmss"));
        int res = commodityMapper.insertComm(commodityBean);
        if (res>0){
            return "添加成功";
        }
        return "添加失败";
    }

    // 查询所有信息
    @Override
    public List<CommodityBean> selectList(CommodityBean commodityBean) {
        return commodityMapper.selectList(commodityBean);
    }

    // 分页查询
    @Override
    public Map<String, Object> limitSelect(int page, int size) {
        List<CommodityBean> commodityBeans = commodityMapper.limitSelect((page-1)*size,size);

        Map<String,Object> restMap = new HashMap<>();
        int count = commodityMapper.selectCount();
        int total=0;
        if(count%size==0){
            total = count/size;
        }else{
            total = count/size+1;
        }
        restMap.put("count",count);
        restMap.put("page",page);
        restMap.put("total",total);
        restMap.put("commList",commodityBeans);
        return restMap;
    }

    // 删除数据
    @Override
    public String deleteCommodity(String no) {
        int result=commodityMapper.deleteCommodity(no);
        if (result == 1) {
            return "删除成功";
        }
        return "删除失败";
    }

    // 更新数据
    @Override
    public String updateCommodity(CommodityBean commodityBean) {
        int rest = commodityMapper.updateCommodity(commodityBean);
        if (rest == 1) {
            return "更新成功";
        }
        return "更新失败";
    }

    // 模糊查询
    @Override
    public Map<String, Object> queryLimitSelect(String c_name, String c_sort, String c_detail, int page, int size) {
        List<CommodityBean> commodityBeans = commodityMapper.queryLimitSelect(c_name, c_sort, c_detail, (page-1)*size,size);
        Map<String,Object> restMap = new HashMap<>();
        int count = commodityMapper.queryLimitSelectCount(c_name, c_sort, c_detail, (page-1)*size,size);
        int total=0;
        if(count%size==0){
            total = count/size;
        }else{
            total = count/size+1;
        }
        restMap.put("count",count);
        restMap.put("page",page);
        restMap.put("total",total);
        restMap.put("commList",commodityBeans);
        return restMap;
    }


}
